package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsPressMachineDetailBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsPressMachineDetailVo;
import com.ruoyi.zhgd.domain.NLaboratoryMechanicsPressMachineDetail;
import com.ruoyi.zhgd.mapper.NLaboratoryMechanicsPressMachineDetailMapper;
import com.ruoyi.zhgd.service.INLaboratoryMechanicsPressMachineDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 压力机详细表Service业务层处理
 *
 * @author zyb
 * @date 2023-09-13
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryMechanicsPressMachineDetailServiceImpl implements INLaboratoryMechanicsPressMachineDetailService {

    private final NLaboratoryMechanicsPressMachineDetailMapper baseMapper;

    /**
     * 查询压力机详细表
     */
    @Override
    public NLaboratoryMechanicsPressMachineDetailVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询压力机详细表列表
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsPressMachineDetailVo> queryPageList(NLaboratoryMechanicsPressMachineDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryMechanicsPressMachineDetail> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsPressMachineDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询压力机详细表列表
     */
    @Override
    public List<NLaboratoryMechanicsPressMachineDetailVo> queryList(NLaboratoryMechanicsPressMachineDetailBo bo) {
        LambdaQueryWrapper<NLaboratoryMechanicsPressMachineDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryMechanicsPressMachineDetail> buildQueryWrapper(NLaboratoryMechanicsPressMachineDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryMechanicsPressMachineDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSampleId()), NLaboratoryMechanicsPressMachineDetail::getSampleId, bo.getSampleId());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleKey()), NLaboratoryMechanicsPressMachineDetail::getSampleKey, bo.getSampleKey());
        lqw.eq(StringUtils.isNotBlank(bo.getDetailKey()), NLaboratoryMechanicsPressMachineDetail::getDetailKey, bo.getDetailKey());
        lqw.eq(StringUtils.isNotBlank(bo.getTime()), NLaboratoryMechanicsPressMachineDetail::getTime, bo.getTime());
        lqw.eq(StringUtils.isNotBlank(bo.getLoad()), NLaboratoryMechanicsPressMachineDetail::getLoad, bo.getLoad());
        lqw.eq(StringUtils.isNotBlank(bo.getStress()), NLaboratoryMechanicsPressMachineDetail::getStress, bo.getStress());
        lqw.eq(StringUtils.isNotBlank(bo.getDatas()), NLaboratoryMechanicsPressMachineDetail::getDatas, bo.getDatas());
        return lqw;
    }

    /**
     * 新增压力机详细表
     */
    @Override
    public Boolean insertByBo(NLaboratoryMechanicsPressMachineDetailBo bo) {
        NLaboratoryMechanicsPressMachineDetail add = BeanUtil.toBean(bo, NLaboratoryMechanicsPressMachineDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改压力机详细表
     */
    @Override
    public Boolean updateByBo(NLaboratoryMechanicsPressMachineDetailBo bo) {
        NLaboratoryMechanicsPressMachineDetail update = BeanUtil.toBean(bo, NLaboratoryMechanicsPressMachineDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryMechanicsPressMachineDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除压力机详细表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 压力机详情列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsPressMachineDetailVo> page(NLaboratoryMechanicsPressMachineDetailBo bo, PageQuery pageQuery) {
        if (StringUtils.isEmpty(bo.getId())) {
            throw new ServiceException("主表id不能为空");
        }
//        LambdaQueryWrapper<NLaboratoryMechanicsPressMachineDetail> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsPressMachineDetailVo> result = baseMapper.page(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }
}
